<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="suanzhang" /><span>湖北志诚教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hshowAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree default-expand-all :data="list">
          <!-- 不是每个作用域插槽都是 row, 具体是什么得根据实际情况打印查看 -->
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%">
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hshowAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hShowEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length === 0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title=" isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="50%"
    >
      <add-or-edit v-if="showDialog" :id="curId" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" @close="hClose" />
    </el-dialog>
    <!-- 编辑的 dialog -->
    <el-dialog
      :title=" isEdit ? '编辑' : '添加'"
      :visible.sync="showDialogEdit"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="50%"
    >
      <add-or-edit v-if="showDialogEdit" :id="curId" :origin-list="originList" :is-edit="isEdit" @success="hSuccess" @close="hClose" />
    </el-dialog>
  </div>
</template>
<script>
import { deleteDepartmentById, getDepartmentList } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '',
      showDialogEdit: false,
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartmentList()
  },
  methods: {
    async  loadDepartmentList() {
      const res = await getDepartmentList()
      // console.log(res)
      res.data.depts.shift()
      this.originList = res.data.depts.map(({ id, pid, name, code }) => ({ id, pid, name, code }))
      this.list = tranListToTreeData(res.data.depts)
    },
    hshowAdd(id) {
      this.showDialog = true
      this.curId = id
      this.isEdit = false
    },
    hSuccess() {
      // 关闭 dialog
      this.showDialog = false
      this.showDialogEdit = false
      // 获取最新的部门信息
      this.loadDepartmentList()
    },
    hShowEdit(id) {
      this.showDialogEdit = true
      this.curId = id
      this.isEdit = true
    },
    hDel(id) {
      // 提醒用户真的要删除吗
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          const res = await deleteDepartmentById(id)
          // 根据结果提醒用户
          this.$message.success(res.message)
          // 重新渲染页面
          this.loadDepartmentList()
        } catch (e) {
          this.$message.error(e.message)
        }
      }).catch(() => {
      })
    },
    hClose() {
      this.hSuccess()
    }
  }
}
</script>
